package henry.lucky.alphay;

import henry.lucky.alphay.Domain.Card;
import henry.lucky.alphay.db.DataBase;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class CardSetting {

	JTextField minTextField=new JTextField("",10);
	JTextField maxTextField=new JTextField("",10);
	private DataBase base=new DataBase();	
	String[] colNames;
	String[][] rowDatas;
	private DefaultTableModel modelCard=null;	
	JTable table=new JTable();
	JProgressBar progressBar;
	//显示界面
	public void show(){
		JFrame frame;
		frame=new JFrame("奖券设置");
		frame.setSize(800, 600);
		frame.setVisible(true);
		//设置框架布局
		Container container=frame.getContentPane();
		JPanel panelTop=new JPanel();
		JPanel panelCenter=new JPanel(new BorderLayout());
		JPanel panelSouth=new JPanel(new BorderLayout());
		JPanel panelDown=new JPanel();
		JPanel panelNorth=new JPanel();
		panelSouth.add(panelDown,BorderLayout.SOUTH);
		panelSouth.add(panelNorth,BorderLayout.NORTH);
		container.add("South",panelSouth);
		container.add("Center",panelCenter);
		container.add("North", panelTop);	
		//设置标题	
		JLabel labelTop=new JLabel("奖券设置");
		Font font=new Font("title", Font.CENTER_BASELINE, 40);
		labelTop.setFont(font);
		panelTop.add(labelTop);	
		//设置显示区域
		int row=0;
		List<Card> cards=base.findCards();
		colNames=new String[]{"奖券编号","是否中过奖"};		
		rowDatas= new String[cards.size()][2];
		for(Card card:cards){
			rowDatas[row][0]=card.getCardName();
			if(card.getIsLuckied()==0){
				rowDatas[row][1]="未中奖";
			}else{
				rowDatas[row][1]="已中奖";
			}
			row++;
		}				
		modelCard=new DefaultTableModel(rowDatas, colNames);
		table.setModel(modelCard);
		panelCenter.add(new JScrollPane(table),BorderLayout.CENTER);
		//显示底部操作区域
		progressBar=new JProgressBar();
		progressBar.setOrientation(JProgressBar.HORIZONTAL);
		JButton clearCardButton=new JButton("清除所有奖券");
		JButton addCardsButton=new JButton("批量添加奖券");
		JButton delCardButton=new JButton("删除选中奖券");
		JLabel minLabel=new JLabel("最小奖券号:");
		JLabel maxLabel=new JLabel("最大奖券号:");
		panelDown.add(clearCardButton);
		panelDown.add(minLabel);
		panelDown.add(minTextField);
		panelDown.add(maxLabel);
		panelDown.add(maxTextField);
		panelDown.add(addCardsButton);
		panelDown.add(delCardButton);
		panelNorth.add(progressBar);
		clearCardButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int init=JOptionPane.showConfirmDialog(null, "你确认要清除所有的奖券吗？此操作将清空所有的奖券");
				if(init==0){
					base.clearCard();
					modelCard.getDataVector().removeAllElements();
					table.revalidate();
					JOptionPane.showMessageDialog(null, "清除所有的奖券成功");					
				}
			}
		});
		addCardsButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String minStr=minTextField.getText();
				String maxStr=maxTextField.getText();
				if(maxStr!=null&&!"".equals(maxStr)&&minStr!=null&&!"".equals(minStr)){
					int min=Integer.valueOf(minStr);
					int max=Integer.valueOf(maxStr);
					progressBar.setMinimum(min);
					progressBar.setMaximum(max);
					new MyThread(min, max).start();
					table.revalidate();
					
				}
				else{
					JOptionPane.showMessageDialog(null, "输入不能为空值");
				}				
			}
		});	
		delCardButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int[] rows=table.getSelectedRows();
				for(int i=0;i<rows.length;i++){
					String cardName=(String)table.getValueAt(rows[i], 0);
					Card card=new Card(cardName);
					base.deleteCard(card);					
				}
				modelCard.getDataVector().removeAllElements();
				List<Card> cards=base.findCards();
				String[] data=new String[2] ;
				for(Card card:cards){
					data[0]=card.getCardName();
					if(card.getIsLuckied()==0){
						data[1]="未中奖";
					}else{
						data[1]="已中奖";
					}
					modelCard.addRow(data);
				}			
				table.revalidate();
			}
		});
	}
	private class MyThread extends Thread{
		int min;
		int max;
		public MyThread(int min, int max) {
			super();
			this.min = min;
			this.max = max;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			String[] data=new String[2];
			for(int i=min;i<=max;i++){
				String currentStr=String.valueOf(i);
				if(i<=9){
					currentStr="00"+currentStr;
				}else if(i<=99){
					currentStr="0"+currentStr;
				}
				Card card=new Card(currentStr);
				base.insertCard(card);
				data[0]=currentStr;
				data[1]="未抽奖";
				modelCard.addRow(data);
				progressBar.setValue(i);
			}
			JOptionPane.showMessageDialog(null, "添加完成");
		}
	}
}
